clear;

[filename, user_canceled] = imgetfile;
if user_canceled
    im = [];
    filename = [];
    return;
end

img = imread(filename);
[w, h, c] = size(img);
if c > 1
    img = rgb2gray(img);
end

N = numel(img);

hist = imhist(img);
hnorm = hist/N;
cdf = cumsum(hnorm);
func = @(x) cdf(x+1);
eqImg = arrayfun(func,img);

figure('name','Histograma');
hold on;
stem(hist);
plot([0:1:255],cdf.*max(hist), 'r');

eqHist = imhist(eqImg);

eqCdf = cumsum(eqHist/N);
figure('name','Histograma equalizado');
hold on;
stem(eqHist);
plot([0:1:255],eqCdf.*max(eqHist), 'r');

figure('name','Imagen original');
imshow(img);
figure('name','Imagen equalizada');
imshow(eqImg);





